package com.android.qjpmc.widget.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.qjpmc.R;
import com.android.qjpmc.databinding.ViewTabViewPageBinding;
import com.android.qjpmc.widget.base.BaseCustomView;
import com.android.qjpmc.widget.base.BaseFragmentAdapter;
import com.android.qjpmc.widget.base.BaseViewPagerAdapter;
import com.android.qjpmc.widget.utils.CommonUtils;
import com.android.qjpmc.widget.utils.DisplayUtils;
import com.android.qjpmc.widget.utils.UIUtils;
import com.android.qjpmc.widget.view.tablayout.TabLayout;

import java.util.List;

import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

/**
 * @Author zhangshuqi
 * @CreateTime 2018/3/27
 * @Describe
 */

public class TabViewPageLayout extends BaseCustomView<ViewTabViewPageBinding> {

    private BaseViewPagerAdapter mAdapter;
    private TabLayout tabLayout;

    public BaseFragmentAdapter getFragmentAdapter() {
        return adapter;
    }

    private BaseFragmentAdapter adapter;
    private List<String> titleList;
    private TabLayout.OnTabSelectedListener tabLayoutOnTabSelectedListener;
    private ViewPager.OnPageChangeListener viewPagerOnPageChangeListener;
    private ViewPager viewPager;

    public TabViewPageLayout(Context context) {
        super(context);
    }

    public TabViewPageLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public void initView(Context context, View rootView, AttributeSet attrs) {

    }

    @Override
    protected void initView(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.TabViewPageLayout);
        tabLayout = new TabLayout(context, attrs);
        tabLayout.setId(R.id.baseTabViewPageLayout);
        tabLayout.setSelectedTabIndicatorColor(UIUtils.getColor(R.color.orange_fd88));
        tabLayout.setSelectedTabIndicatorHeight((int) DisplayUtils.getDimension(R.dimen.dp_4));
        int tabLayoutWidth = typedArray.getInteger(R.styleable.TabViewPageLayout_tabLayoutWidth, ViewGroup.LayoutParams.MATCH_PARENT);
        int tabLayoutGravity = typedArray.getInteger(R.styleable.TabViewPageLayout_tabLayoutGravity, Gravity.LEFT);

        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(tabLayoutWidth, ViewGroup.LayoutParams.WRAP_CONTENT);
        params.height = (int) UIUtils.getResource().getDimension(R.dimen.dp_44);
        params.gravity = tabLayoutGravity;
        mBinding.llContent.addView(tabLayout, 0, params);
        LinearLayout.LayoutParams viewParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        int viewPageType = typedArray.getInteger(R.styleable.TabViewPageLayout_viewPageType, 0);

        if (viewPageType == 0) {
            viewPager = new ViewPager(context);
        } else if (viewPageType == 1) {
            viewPager = new ViewPagerFixed(context);
        } else if (viewPageType == 2) {
            viewPager = new NoScrollViewPager(context);
        }
        viewPager.setId(R.id.baseViewPageLayout);
        viewPager.setLayoutParams(viewParams);
        mBinding.llContent.addView(viewPager);
        typedArray.recycle();
    }

    @Override
    public void initData(Context context) {

    }

    @Override
    public void initEvent(Context context) {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.view_tab_view_page;
    }

    public TabLayout getTabLayout() {
        return tabLayout;
    }

    public ViewPager getViewPager() {
        return viewPager;
    }

    public void setCurrentItem(int currentItem) {
        viewPager.setCurrentItem(currentItem);
    }


    public void setTabLayoutVisibility(boolean f) {
        if (tabLayout != null)
            tabLayout.setVisibility(f ? VISIBLE : GONE);
    }

    public void setViewPageData(final int layoutId, final List<String> titleList, List dataList) {
        mAdapter = new BaseViewPagerAdapter(dataList) {
            @Override
            public View getRootView() {
                return View.inflate(getContext(), layoutId, null);
            }

            @Override
            public CharSequence getPageTitle(int position) {
                return titleList.get(position);
            }
        };
        viewPager.setAdapter(mAdapter);
        tabLayout.setupWithViewPager(viewPager);
    }

    public void setFragmentViewPageData(FragmentActivity appCompatActivity, List<String> titleList, List dataList) {
        setFragmentAdapter(appCompatActivity, titleList, dataList);
    }

    public void setFragmentViewPageData(FragmentActivity appCompatActivity, List dataList) {
        setFragmentAdapter(appCompatActivity, null, dataList);
    }

    private void setFragmentAdapter(FragmentActivity appCompatActivity, List<String> titleList, List dataList) {
        this.titleList = titleList;
        adapter = new BaseFragmentAdapter(appCompatActivity.getSupportFragmentManager(), titleList, dataList);
        viewPager.setAdapter(adapter);
        tabLayout.setupWithViewPager(viewPager);

    }

    public void setCustomViewTabLayoutView(FragmentManager fragmentManager, List dataList) {
        adapter = new BaseFragmentAdapter(fragmentManager, dataList);
        viewPager.setAdapter(adapter);
        updateTabTextView(getTabLayout().getTabAt(0), true);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                if (viewPagerOnPageChangeListener != null) {
                    viewPagerOnPageChangeListener.onPageScrolled(position, positionOffset, positionOffsetPixels);
                }
            }

            @Override
            public void onPageSelected(int position) {
                getTabLayout().getTabAt(position).select();
                if (viewPagerOnPageChangeListener != null) {
                    viewPagerOnPageChangeListener.onPageSelected(position);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                if (viewPagerOnPageChangeListener != null) {
                    viewPagerOnPageChangeListener.onPageScrollStateChanged(state);
                }
            }
        });
        // viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(mBinding.tabLayout));
        getTabLayout().addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                updateTabTextView(tab, true);
                viewPager.setCurrentItem(tab.getPosition());
                if (tabLayoutOnTabSelectedListener != null) {
                    tabLayoutOnTabSelectedListener.onTabSelected(tab);
                }

            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                updateTabTextView(tab, false);
                if (tabLayoutOnTabSelectedListener != null) {
                    tabLayoutOnTabSelectedListener.onTabUnselected(tab);
                }
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
                if (tabLayoutOnTabSelectedListener != null) {
                    tabLayoutOnTabSelectedListener.onTabReselected(tab);
                }
            }
        });
    }

    public void setCustomViewTabLayoutView(FragmentActivity fragmentActivity, List<String> titleList, List dataList) {
        setCustomViewTabLayoutView(fragmentActivity.getSupportFragmentManager(), titleList, dataList);
    }
    public void setCustomViewTabLayoutView(FragmentManager fragmentManager, List<String> titleList, List dataList) {
        setCustomViewTabLayoutView(fragmentManager,titleList,dataList, FragmentPagerAdapter.BEHAVIOR_SET_USER_VISIBLE_HINT);
    }
    public void setCustomViewTabLayoutView(FragmentManager fragmentManager, List<String> titleList, List dataList,int behavior) {
        this.titleList = titleList;
        for (int i = 0; i < titleList.size(); i++) {
            final TabLayout.Tab tab = getTabLayout().newTab();
            tab.setCustomView(getTabView(i));
            final View tabView = (View) tab.getCustomView().getParent();
            tabView.setTag(i);
            getTabLayout().addTab(tab);
        }
        adapter = new BaseFragmentAdapter(fragmentManager, dataList,behavior);
        viewPager.setAdapter(adapter);
       // viewPager.setOffscreenPageLimit(titleList.size());
        updateTabTextView(getTabLayout().getTabAt(0), true);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                if (viewPagerOnPageChangeListener != null) {
                    viewPagerOnPageChangeListener.onPageScrolled(position, positionOffset, positionOffsetPixels);
                }
            }

            @Override
            public void onPageSelected(int position) {
                getTabLayout().getTabAt(position).select();
                if (viewPagerOnPageChangeListener != null) {
                    viewPagerOnPageChangeListener.onPageSelected(position);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                if (viewPagerOnPageChangeListener != null) {
                    viewPagerOnPageChangeListener.onPageScrollStateChanged(state);
                }
            }
        });
        // viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(mBinding.tabLayout));
        getTabLayout().addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                updateTabTextView(tab, true);
                viewPager.setCurrentItem(tab.getPosition());
                if (tabLayoutOnTabSelectedListener != null) {
                    tabLayoutOnTabSelectedListener.onTabSelected(tab);
                }

            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                updateTabTextView(tab, false);
                if (tabLayoutOnTabSelectedListener != null) {
                    tabLayoutOnTabSelectedListener.onTabUnselected(tab);
                }
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
                if (tabLayoutOnTabSelectedListener != null) {
                    tabLayoutOnTabSelectedListener.onTabReselected(tab);
                }
            }
        });
    }

    public void setViewPagerOnPageChangeListener(ViewPager.OnPageChangeListener pagerOnPageChangeListener) {
        this.viewPagerOnPageChangeListener = pagerOnPageChangeListener;
    }

    public void addOnTabSelectedListener(TabLayout.OnTabSelectedListener pagerOnPageChangeListener) {
        this.tabLayoutOnTabSelectedListener = pagerOnPageChangeListener;
    }

    /**
     * @param currentPosition
     * @return
     */
    private View getTabView(final int currentPosition) {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.base_tab_layout, null);
        TextView textView = view.findViewById(R.id.tv_title);
        textView.setText(titleList.get(currentPosition));
        textView.setTextSize(tabUnSize);
        return view;
    }

    public int getTabColor() {
        return tabColor;
    }

    public void setTabColor(int tabColor) {
        this.tabColor = tabColor;
    }

    public int getTabUnColor() {
        return tabUnColor;
    }

    public void setTabUnColor(int tabUnColor) {
        this.tabUnColor = tabUnColor;
    }

    int tabColor=R.color.tabColor;
    int tabUnColor=R.color.tabUnColor;

    public void setTabUnSize(int tabUnSize) {
        this.tabUnSize = tabUnSize;
    }

    public void setTabSize(int tabSize) {
        this.tabSize = tabSize;
    }

    int tabUnSize=16;
    int tabSize=16;
    public void updateTabTextView(TabLayout.Tab tab, boolean isSelect) {
        TextView tabSelect = tab.getCustomView().findViewById(R.id.tv_title);
        tabSelect.setTypeface(Typeface.defaultFromStyle(isSelect ? Typeface.BOLD : Typeface.NORMAL));
        tabSelect.setTextColor(UIUtils.getColor(isSelect ? tabColor :tabUnColor));
        tabSelect.setTextSize( isSelect ? tabSize : tabUnSize );
    }

    public void updateTabTextView(int tab, boolean isSelect) {
        updateTabTextView(getTabLayout().getTabAt(tab), isSelect);
    }

    public void setFragmentViewPageData(FragmentActivity appCompatActivity, String[] titles, List dataList) {
        setFragmentAdapter(appCompatActivity, null, dataList);
    }

    public void setIndicator(int l, int r) {
        CommonUtils.setIndicator(getTabLayout(), l, r);
    }

    public void setTabLayoutModel(int model) {
        getTabLayout().setTabMode(model);
    }

    public void setTabGravity(int gravityFill) {
        getTabLayout().setTabGravity(gravityFill);
    }


    public void setPageAdapterItemDecorator(BaseViewPagerAdapter.BaseViewPageItemDecorator decorator) {
        if (mAdapter != null)
            mAdapter.setItemDecorator(decorator);
    }

    public void setPageAdapterItemPresenter(BaseViewPagerAdapter.BaseViewPageItemPresenter presenter) {
        if (mAdapter != null)
            mAdapter.setItemPresenter(presenter);
    }

    public void notifyPageAdapterDataSetChanged() {
        if (mAdapter != null)
            mAdapter.notifyDataSetChanged();

    }

    public void notifyFragmentAdapterDataSetChanged() {
        if (adapter != null)
            adapter.notifyDataSetChanged();

    }

    public void remove() {
        if (titleList != null)
            titleList.clear();
        adapter.notifyDataSetChanged();
        adapter = null;
        if (tabLayout != null)
            tabLayout.removeAllTabs();
    }

    public void setOffscreenPageLimit(int i) {
        viewPager.setOffscreenPageLimit(i);
    }

 /*   public AppBarLayout getAppBarLayout() {
        return mBinding.appbarLayout;
    }*/
}
